// App.js
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import './App.css';

import LoginPage from './components/LoginPage';
import Chatbot from './components/Chatbot';
import KnowledgeBase from './components/KnowledgeBase';
import IntentRecognition from './components/IntentRecognition';
import SentimentAnalysis from './components/SentimentAnalysis';

function App() {
  const [loggedIn, setLoggedIn] = useState(false);
  const [currentPage, setCurrentPage] = useState('chatbot');
  const navigate = useNavigate();

  const handleLogin = () => {
    setLoggedIn(true);
    setCurrentPage('chatbot');
    navigate('/'); // 登录成功后导航到主页面
  };

  const handleLogout = () => {
    setLoggedIn(false);
    navigate('/login'); // 登出后导航到登录页面
  };

  const handlePageChange = (page) => {
    setCurrentPage(page);
  };

  const handleClose = () => {
    // 定义关闭逻辑
    console.log('Closing login');
  };

  if (!loggedIn) {
    return <LoginPage onLogin={handleLogin} />;
  }

  let currentPageComponent;
  switch (currentPage) {
    case 'chatbot':
      currentPageComponent = <Chatbot />;
      break;
    case 'knowledgeBase':
      currentPageComponent = <KnowledgeBase />;
      break;
    case 'intentRecognition':
      currentPageComponent = <IntentRecognition />;
      break;
    case 'sentimentAnalysis':
      currentPageComponent = <SentimentAnalysis />;
      break;
    default:
      currentPageComponent = <Chatbot />;
      break;
  }

  return (
    <div className="App">
      <header>
        <nav>
          <button onClick={() => handlePageChange('chatbot')}>智能聊天</button>
          <button onClick={() => handlePageChange('knowledgeBase')}>知识库管理</button>
          <button onClick={() => handlePageChange('intentRecognition')}>用户意图识别</button>
          <button onClick={() => handlePageChange('sentimentAnalysis')}>用户情感分析</button>
          <button onClick={handleLogout}>退出</button>
        </nav>
      </header>
      <main>
        {currentPageComponent}
      </main>
    </div>
  );
}

export default App;
